@extends('admin.base')
@section('content')


    <div class="layui-form" onsubmit="return false" style="padding:20px;">
        <div class="layui-form-item layui-form-text"></div>

        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="password"  name="old_password"  lay-verify="required" placeholder="密码" autocomplete="off" class="layui-input">
            </div>

        </div>
        <div>
            <label class="layui-form-label">确定密码</label>
            <div class="layui-input-inline">
                <input type="password" name="new_password"  lay-verify="required" placeholder="确定密码" autocomplete="off" class="layui-input" lay-verify="required|number">
            </div>

        </div>


        <div class="layui-form-item"></div>


        <div class="layui-form-item layui-form-text"></div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="submit_reset">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </div>


    @section('script')
        @can('supplier.manage')
            <script>
                layui.use(['form', 'table','upload','element',], function () {
                    var $ = layui.jquery,
                        form = layui.form,
                        table = layui.table,
                        upload = layui.upload;
                    form.verify({
                        title: function (value, item) {
                            if (value.length < 5) {
                                return '标题至少得5个字符啊';
                            }
                        }, fname: function (value) {
                            if (value.length < 4) {
                                return '请输入至少4位的用户名';
                            }
                        }, contact: function (value) {
                            if (value.length < 4) {
                                return '内容请输入至少4个字符';
                            }
                        }
                        , phone: [/^1[3|4|5|7|8]\d{9}$/, '手机必须11位，只能是数字！']
                        , email: [/^[a-z0-9._%-]+@([a-z0-9-]+\.)+[a-z]{2,4}$|^1[3|4|5|7|8]\d{9}$/, '邮箱格式不对']
                    });



                    // 监听搜索操作
                    form.on('submit(data-search-btn)', function (data) {
                        var result = JSON.stringify(data.field);
                        layer.alert(result, {
                            title: '最终的搜索信息'
                        });

                        //执行搜索重载
                        table.reload('currentTableId', {
                            page: {
                                curr: 1
                            }
                            , where: {
                                searchParams: result
                            }
                        }, 'data');

                        return false;
                    });


                    form.on('submit(submit_reset)', function (data) {
                        data = data.field;
                        // 验证new_password 必须是6位
                        if(data.new_password.length < 6){
                            layer.msg('密码必须大于6位!');
                            return false;
                        }
                        if(data.new_password != data.old_password){
                            layer.msg('输入密码不一致!');
                            return false;
                        }

                        $.ajax({
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                            type: "post",
                            url: "{{ route('admin.user.updatePassword') }}",
                            data:data,
                            dataType: "json",
                            success: function (arg) {
                                if (arg.code == 0) {
                                    var index = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(index);//关闭当前页
                                    window.parent.location.reload();
                                    window.location.href = '';
                                } else {
                                    layer.alert(arg.errmsg);
                                }
                            },
                        });
                        return false;
                    });
                });
            </script>

        @endcan
    @endsection